package com.ccrfid.rmc6.service.personnel;

import com.ccrfid.rmc6.entity.personnel.PersonnelMutualGroupEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelMutualGroupMemberEntity;
import com.ccrfid.rmc6.service.amq.MqMsgProducer;
import com.ccrfid.rmc6.util.JsonUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MutualGroupServiceAspect {
    private static final Logger logger = LoggerFactory.getLogger(MutualGroupServiceAspect.class);
    @Autowired
    private MqMsgProducer mqMsgProducer;

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.MutualGroupService.createPersonnelMutualGroup(..))",
            returning = "ret")
    public void afterCreatePersonnelMutualGroup(PersonnelMutualGroupEntity ret) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(ret);
        mqMsgProducer.send2Topic("ws_mutual_group_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.MutualGroupService.updatePersonnelMutualGroup(..))",
            returning = "ret")
    public void afterUpdatePersonnelMutualGroup(PersonnelMutualGroupEntity ret) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(ret);
        mqMsgProducer.send2Topic("ws_mutual_group_update", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.MutualGroupService.deletePersonnelMutualGroup(..)) && args(groupId)",
            argNames = "groupId")
    public void afterDeletePersonnelMutualGroup(Long groupId) {
        String msg = "[" + groupId + "]";
        mqMsgProducer.send2Topic("ws_mutual_group_delete", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.MutualGroupService.createPersonnelMutualGroupMember(..))",
            returning = "ret")
    public void afterCreatePersonnelMutualGroupMember(PersonnelMutualGroupMemberEntity ret) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(ret);
        mqMsgProducer.send2Topic("ws_mutual_group_member_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.MutualGroupService.deletePersonnelMutualGroupMember(..)) && args(groupId, memberId)",
            argNames = "groupId, memberId")
    public void afterDeletePersonnelMutualGroupMember(Long groupId, Long memberId) {
        String msg = "{\"groupId\":" + groupId + ",\"memberId\":" + memberId + "}";
        mqMsgProducer.send2Topic("ws_mutual_group_member_delete", msg);
    }
}
